import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
		
public class Overworld {
	Hex[] world = new Hex[49];
	Hex overWorldLocation;
	Area city = new Area("Figaro","city","FCity");
	Area currentArea = null;
	
	public Overworld(){
		
		generateWorld();
		
		// Set overworld location
		overWorldLocation = world[24];
		
		city.buildArea(null);
		city.enterArea('n');
	}
	
	public void generateWorld(){
		int x = 0;
		
		for(int i=-3;i<4;i++){
			for(int j=-3;j<4;j++){
				world[x] = new Hex(i,j,false,"");
				x++;
			}
		}
		
		System.out.println("City at " + world[20]);
		world[20].containsArea = true;
	}
	
	public Hex getCurrentLocation(){
		return overWorldLocation;
	}
	
	public Hex getHexByCoords(int x, int y){
		Hex returnHex = null;

		for(int i=0;i<world.length;i++){
			if(x == world[i].x && y == world[i].y){
				returnHex = world[i];
			}
		}

		return returnHex;
	}
	
	public int getDistance(int q1, int r1, int q2, int r2){
		    return (Math.abs(q1 - q2) + Math.abs(r1 - r2) + Math.abs(q1 + r1 - q2 - r2)) / 2;
	}
	
	public void pathFind(int q1, int r1, int q2, int r2){
		//Definitely not even close
		//Filter out unpassable neighbors if applicable
		getDistance(q1,r1,q2,r2);
	}
	
	public void showNeighbors(){
		Hex[] neighbors = new Hex[6];
		neighbors[0] = new Hex(overWorldLocation.x+1,overWorldLocation.y);
		neighbors[1] = new Hex(overWorldLocation.x+1, overWorldLocation.y-1);
		neighbors[2] = new Hex(overWorldLocation.x, overWorldLocation.y-1);
		neighbors[3] = new Hex(overWorldLocation.x-1, overWorldLocation.y+1);
		neighbors[4] = new Hex(overWorldLocation.x-1, overWorldLocation.y);
		neighbors[5] = new Hex(overWorldLocation.x, overWorldLocation.y+1);
		
		for(int i=0;i<neighbors.length;i++){
			System.out.println(neighbors[i]);
		}
	}
	
	public void moveOverWorldDirection(String direction){
		int yMovement = 0;
		int xMovement = 0;
		
		/**
		 * 1 - Check if hex tile exists
		 * 2 - Check if terrain can be passed
		 * 3 - Check if area tile and prompt if player wants to enter
		 * 	3a - Determine direction entered from to decide where in area player begins
		 * 4 - If all above met, move tile
		 */
		
		if(direction.equals("n"))
			yMovement--;
		else if(direction.equals("s"))
			yMovement++;
		else if(direction.equals("ne")){
			xMovement++;
			yMovement--;
		}
		else if(direction.equals("nw")){
			xMovement--;
		}
		else if(direction.equals("se")){
			xMovement++;
		}
		else if(direction.equals("sw")){
			xMovement--;
			yMovement++;
		}
			
		// Get Tile
		Hex tempHex = getHexByCoords((overWorldLocation.x+xMovement),(overWorldLocation.y+yMovement));
		
		// Check if Tile exists
		if(tempHex != null){
			// Check if terrain can be passed
			//if(tempHex.terrain.passable){
				// Check if Tile contains an Area
				if(tempHex.containsArea){
					System.out.println("Do you wish to enter the area?");
					System.out.println("Now entering city!");
					currentArea = city;
				}
				overWorldLocation = getHexByCoords((overWorldLocation.x+xMovement),(overWorldLocation.y+yMovement));
		}
		else{
			System.out.println("There is nothing in that direction!");
			//JOptionPane.showMessageDialog(null, "There is nothing in that direction!");
		}
		
		
	}
}
